var content;
var loading;
var pages = new HashTable(); // or just {}
var solucoes = new HashTable(); // or just {}

if (window.addEventListener) {
	window.addEventListener('load', init, false);
} else if (window.attachEvent) {
	window.attachEvent('onload', init);
}

function HashTable(obj)
{
    this.length = 0;
    this.items = {};
    for (var p in obj) {
        if (obj.hasOwnProperty(p)) {
            this.items[p] = obj[p];
            this.length++;
        }
    }

    this.setItem = function(key, value)
    {
        var previous = undefined;
        if (this.hasItem(key)) {
            previous = this.items[key];
        }
        else {
            this.length++;
        }
        this.items[key] = value;
        return previous;
    };

    this.getItem = function(key) {
        return this.hasItem(key) ? this.items[key] : undefined;
    };

    this.hasItem = function(key)
    {
        return this.items.hasOwnProperty(key);
    };
   
    this.removeItem = function(key)
    {
        if (this.hasItem(key)) {
            previous = this.items[key];
            this.length--;
            delete this.items[key];
            return previous;
        }
        else {
            return undefined;
        }
    };

    this.keys = function()
    {
        var keys = [];
        for (var k in this.items) {
            if (this.hasItem(k)) {
                keys.push(k);
            }
        }
        return keys;
    }

    this.values = function()
    {
        var values = [];
        for (var k in this.items) {
            if (this.hasItem(k)) {
                values.push(this.items[k]);
            }
        }
        return values;
    }

    this.each = function(fn) {
        for (var k in this.items) {
            if (this.hasItem(k)) {
                fn(k, this.items[k]);
            }
        }
    }

    this.clear = function()
    {
        this.items = {}
        this.length = 0;
    }
}

function events() {

	$("#main_nav li a").click(function(e) {

		try {
			
			e.preventDefault();
			
			var currentPage = $('#main_nav li a.selected').attr('href');
			var newPage = $(this).attr('href');

			if (currentPage == newPage) {
				return false;
			}

			$('#main_nav li a.selected').removeClass('selected');
			$(this).addClass('selected');

			loadContent(newPage);

			return false;

		} catch (e) {
			alert(e.message);
			return true;
		}

	});

}
function init() {	
	loadScripts();
}

function showLoading() {
	loading.fadeIn();
	return false;
}

function hideLoading() {
	loading.fadeOut('fast');
	return false;
}

function loadRequiredScripts(callback) {

	loadScript("http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js", function(){
		
		content = $("#main_content");
		loading = $("#loading");
		
		events();

		loadCss("http://logicart-website.googlecode.com/svn/includes/slidesjs/slides.css");
		loadScript("http://logicart-website.googlecode.com/svn/includes/slidesjs/slides.min.jquery.js",function() {
			loadContent('home');
			loadSecondaryScripts();
		});		
		
	});


}

function loadSecondaryScripts() {
	
	loadScript("http://logicart-website.googlecode.com/svn/includes/simplemodal/jquery.simplemodal.1.4.3.min.js");
	loadScript("http://logicart-website.googlecode.com/svn/includes/danielstocks-jQuery-Placeholder/jquery.placeholder.min.js");

	loadCss("http://logicart-website.googlecode.com/svn/includes/colorbox/colorbox.css");
	loadScript("http://logicart-website.googlecode.com/svn/includes/colorbox/jquery.colorbox-min.js");

}

function loadScripts() {
	loadRequiredScripts(loadSecondaryScripts);
}

function loadContent(page) {

	try{
		
//		alert(0);
//		content.hide();
//		showLoading();
	
//		var html = pages.getItem(page);
//		if(html == null){
//			alert(1);
			var finalPage = "pages/" + page + ".php";
//			$.get(finalPage, function(data){
//				alert(3);
//				pages.setItem(page, data);
//				alert(4);
//				content.html(data);
//				alert(5);
//				hideLoading();
//				alert(6);
//				content.fadeIn("slow");
//				alert(7);
//				
//			});
//		}else{
//			alert(2);
//			content.html(html);
//			hideLoading();
//			content.fadeIn("slow");
//		}
		
//		content.load(finalPage, function() {
//			hideLoading();
//			content.fadeIn("slow");
//		});
	
//			hideLoading();
			content.load(finalPage);
//			content.fadeIn("slow");
			
			
		return false;
	}catch (e) {
		alert(e.message);
	}
}

function loadCss(source, callback) {

	var css = document.createElement("link");
	css.setAttribute("rel", "stylesheet");
	css.setAttribute("type", "text/css");
	// css.setAttribute('async', 'true');

//	if (callback != null) {
//		if (css.readyState) {
//			css.onreadystatechange = function() {
//				if (css.readyState == "loaded" || css.readyState == "complete") {
//					css.onreadystatechange = null;
//					callback();
//				}
//			};
//		} else {
//			css.onload = function() {
////				alert(11);
//				callback();
//			};
//		}
//	}

	addEvent(css, 'load', callback, false);
	
	css.setAttribute("href", source);
	document.getElementsByTagName("head")[0].appendChild(css);

}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) { 
		elm.addEventListener(evType, fn, useCapture); 
		return true; 
	}
	else if (elm.attachEvent) { 
		var r = elm.attachEvent('on' + evType, fn); 
		return r; 
	}
	else {
		elm['on' + evType] = fn;
	}
}

function loadScript(source, callback) {

	var js = document.createElement('script');
	js.setAttribute("type", "text/javascript");
	// js.setAttribute('async', 'true');

	if (callback != null) {
		if (js.readyState) {
			js.onreadystatechange = function() {				
				if (js.readyState == "loaded" || js.readyState == "complete") {
					js.onreadystatechange = null;
					callback();
				}
			};
		} else {
			js.onload = function() {
				callback();
			};
		}
	}

	js.setAttribute("src", source);
	document.getElementsByTagName("head")[0].appendChild(js);

}

// <!-- jReject !-->
// <!-- <link rel="stylesheet" href="includes/jReject/css/jquery.reject.css" /> -->
// <!-- <script type="text/javascript" src="includes/jReject/js/jquery.reject.min.js"></script> -->
